package com.cx.api.holder;

/**
 * @author: zjm
 * @create: 2022-06-12 19:07
 **/

import com.alibaba.fastjson.JSON;
import com.cx.auth.model.vo.UserDetailsVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

@Component
public class LoginUserHolder {
    public UserDetailsVO getCurrentUser(){
        //从Header中获取用户信息
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = servletRequestAttributes.getRequest();
        String userStr = request.getHeader("user");
        if (StringUtils.isEmpty(userStr)){
            return null;
        }
        UserDetailsVO user = JSON.parseObject(userStr, UserDetailsVO.class);
        return user;
    }
}
